package com.aat.dxfy.utils;

import java.security.Security;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailSendUtil {
	public static String account = "admin@dizena.com";
	public static String passWord = "AatDxy1236";
	public static String smtp = "smtp.dizena.com";

	public static void main(String[] args) {
		String email = "wuxin@umessage.com.cn";
		String content = "验证码：1368";
		String title ="北大计算中心网络服务验证";
		sendMailSSL(email, content, title);
	}

	private EmailSendUtil() {

	}

	@SuppressWarnings("restriction")
	public static boolean sendMailSSL(String email, String content, String title) {
		// 设置SSL连接、邮件环境
		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
		final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
		Properties props = System.getProperties();
		props.setProperty("mail.smtp.host", smtp);
		props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
		props.setProperty("mail.smtp.socketFactory.fallback", "false");
		props.setProperty("mail.smtp.port", "465");
		props.setProperty("mail.smtp.socketFactory.port", "465");
		props.setProperty("mail.smtp.auth", "true");
		// 建立邮件会话
		Session session = Session.getDefaultInstance(props, new Authenticator() {
			// 身份认证
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(account, passWord);// 获取用户名和密码
			}
		});

		Message msg = new MimeMessage(session);
		session.setDebug(false);// 查看调试信息:true,不查看：false;
		try {
			msg.setFrom(new InternetAddress(account));
			msg.setSubject(title);
			msg.setRecipients(RecipientType.TO, InternetAddress.parse(email));// 多个收件人
			msg.setContent(content, "text/html;charset=utf-8");// 文本/指定文本类型，字符集
			Transport.send(msg);
		} catch (javax.mail.MessagingException e) {
			e.printStackTrace();
			// 发送失败
			return false;
		} // 发送端
		return true;
	}

	public static boolean sendMail(String email, String content, String title) {
		Properties props = new Properties();
		props.setProperty("mail.smtp.auth", "true");
		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.host", smtp);// 用户主机
		Session session = Session.getInstance(props, new Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(account, passWord);// 获取用户名和密码
			}
		});

		Message msg = new MimeMessage(session);
		session.setDebug(false);// 查看调试信息:true,不查看：false;
		try {
			msg.setFrom(new InternetAddress(account));
			msg.setSubject(title);
			msg.setRecipients(RecipientType.TO, InternetAddress.parse(email));// 多个收件人
			msg.setContent(content, "text/html;charset=utf-8");// 文本/指定文本类型，字符集
			Transport.send(msg);
		} catch (javax.mail.MessagingException e) {
			e.printStackTrace();
			// 发送失败
			return false;
		} // 发送端
		return true;
	}
}
